package com.luckynote.common.user;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.luckynote.common.base.exception.BizException;
import com.luckynote.common.base.result.ExecuteResult;
import com.luckynote.common.base.result.MultiEntryResult;
import com.luckynote.common.base.result.ResponseInfo;
import com.luckynote.common.base.util.BeanUtil;
import com.luckynote.common.base.util.StringUtil;
import com.luckynote.common.user.dto.UserDTO;
import com.luckynote.common.user.mapper.UserDTOMapper;
import com.luckynote.common.user.query.UserQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * <pre>
 * *********************************************
 * Copyright LUCKY-NOTE.
 * All rights reserved.
 * ****************************************************
 *  Version       Date         Author            Desc
 *    v1.0       2019/6/29     bingo           用户相关信息
 * ****************************************************
 * </pre>
 */
public class UserServiceImpl implements UserService {

    private static Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    private UserDTOMapper userDTOMapper;

    /**
     * 新增或编辑
     *
     * @param userDTO 用户信息
     * @return 执行结果
     */
    @Override
    public ExecuteResult saveOrUpdate(UserDTO userDTO) {
        ExecuteResult result = new ExecuteResult();
        if(StringUtil.isEmptyString(userDTO.getUserId())){
            userDTO.setUserId(StringUtil.UUID());
            userDTOMapper.insertSelective(userDTO);
        }else{
            userDTOMapper.updateByPrimaryKey(userDTO);
        }
        return result;
    }

    /**
     * 根据用户ID删除对应信息
     *
     * @param userId 用户ID
     * @return 执行结果
     */
    @Override
    public ExecuteResult delete(String userId) {
        ExecuteResult result = new ExecuteResult();
        if(StringUtil.isEmptyString(userId)){
            throw new BizException(new ResponseInfo("400","参数不能为空!"));
        }
        userDTOMapper.deleteByPrimaryKey(userId);
        return result;
    }

    /**
     * 获取用户列表信息
     *
     * @param userQuery 通用查询类
     * @return 返回用户信息列表
     */
    @Override
    public MultiEntryResult<UserDTO> list(UserQuery userQuery) {
        logger.info("user list .receive param: =>[{}]",BeanUtil.toJson(userQuery));
        MultiEntryResult result = new MultiEntryResult();
        PageHelper.startPage(userQuery.getPageNo(), userQuery.getPageSize());
        List<UserDTO> list =  userDTOMapper.selectByUserQuery(userQuery);
        PageInfo<UserDTO> pageInfo = new PageInfo<>(list);
        result.setList(pageInfo.getList());
        if(pageInfo.getList().size() > 0){
            throw new RuntimeException("自定义异常~");
        }
        result.setCount(Integer.parseInt(pageInfo.getTotal() + ""));
        result.setPageNO(pageInfo.getPageNum());
        result.setPageSize(pageInfo.getPageSize());
        return result;
    }

    public UserDTOMapper getUserDTOMapper() {
        return userDTOMapper;
    }

    public void setUserDTOMapper(UserDTOMapper userDTOMapper) {
        this.userDTOMapper = userDTOMapper;
    }
}
